from PySide6.QtCore import Slot, Signal
from PySide6.QtGui import QAction, QActionGroup
from PySide6.QtWidgets import QMenu

from utils.config import Settings
from utils.log import logger


class MainMenu(QMenu):
    set_trans = Signal(str, str)
    set_enable = Signal(bool)

    def __init__(self, app):
        super().__init__()
        self.app = app
        self.settings = Settings()

        # 开启关闭翻译
        self.act_enable = QAction('开启翻译', self)
        self.act_enable.setCheckable(True)
        self.act_enable.setChecked(self.settings.enable)
        self.act_enable.triggered.connect(self.on_act_enable_clicked)
        self.addAction(self.act_enable)

        # 翻译模式
        self.act_group_trans_mode = QActionGroup(self)
        self.act_group_trans_mode.setExclusive(True)  # 只能选择一个
        self.menu_trans_mode = QMenu('翻译模式', self)

        self.act_auto = QAction('自动检测', self.menu_trans_mode)
        self.act_auto.setCheckable(True)
        self.act_auto.triggered.connect(self.on_act_auto_clicked)
        self.act_group_trans_mode.addAction(self.act_auto)

        self.act_zh_en = QAction('中英互译', self.menu_trans_mode)
        self.act_zh_en.setCheckable(True)
        self.act_zh_en.triggered.connect(self.on_act_zh_en_clicked)
        self.act_group_trans_mode.addAction(self.act_zh_en)

        self.act_zh_jp = QAction('中日互译', self.menu_trans_mode)
        self.act_zh_jp.setCheckable(True)
        self.act_zh_jp.triggered.connect(self.on_act_zh_jp_clicked)
        self.act_group_trans_mode.addAction(self.act_zh_jp)

        self.menu_trans_mode.addActions([self.act_auto, self.act_zh_en, self.act_zh_jp])
        self.addMenu(self.menu_trans_mode)

        # 翻译 API
        self.act_group_trans_api = QActionGroup(self)
        self.act_group_trans_api.setExclusive(True)  # 只能选择一个
        self.menu_trans_api = QMenu('翻译 API', self)

        self.act_baidu = QAction('百度翻译', self.menu_trans_api)
        self.act_baidu.setCheckable(True)
        self.act_baidu.triggered.connect(self.on_act_baidu_clicked)
        self.act_group_trans_api.addAction(self.act_baidu)

        self.act_youdao = QAction('有道智云', self.menu_trans_api)
        self.act_youdao.setCheckable(True)
        self.act_youdao.triggered.connect(self.on_act_youdao_clicked)
        self.act_group_trans_api.addAction(self.act_youdao)

        self.menu_trans_api.addActions([self.act_baidu, self.act_youdao])
        self.addMenu(self.menu_trans_api)

        # 初始化值
        self.set_trans_handler(self.settings.trans_mode, self.settings.trans_api)

        # 设置
        self.act_settings = QAction('设置', self)
        self.act_settings.triggered.connect(self.on_act_settings_clicked)
        self.addAction(self.act_settings)

        # 连接退出动作的槽
        self.act_quit = QAction('退出', self)
        self.act_quit.triggered.connect(self.on_act_quit_clicked)
        self.addAction(self.act_quit)

    def connect_slots(self):
        self.set_trans.connect(self.app.settings_window.tab_general.set_trans_handler)
        self.set_enable.connect(self.app.clipboard_monitor.set_enable_handler)

    def on_act_enable_clicked(self):
        logger.debug('Handle act_enable clicked')
        self.app.clipboard_monitor.enable ^= True
        enable = self.app.clipboard_monitor.enable
        self.act_enable.setChecked(enable)
        self.set_enable.emit(enable)

    def on_act_auto_clicked(self):
        logger.debug('Handle act_auto clicked')
        self.settings.trans_mode = 'auto'
        self.set_trans_mode_handler('auto')

    def on_act_zh_en_clicked(self):
        logger.debug('Handle act_zh_en clicked')
        self.settings.trans_mode = 'zh_en'
        self.set_trans_mode_handler('zh_en')

    def on_act_zh_jp_clicked(self):
        logger.debug('Handle act_zh_jp clicked')
        self.settings.trans_mode = 'zh_jp'
        self.set_trans_mode_handler('zh_jp')

    def on_act_baidu_clicked(self):
        logger.debug('Handle act_baidu clicked')
        self.settings.trans_api = 'Baidu'
        self.set_trans_api_handler('Baidu')

    def on_act_youdao_clicked(self):
        logger.debug('Handle act_youdao clicked')
        self.settings.trans_api = 'Youdao'
        self.set_trans_api_handler('Youdao')

    def on_act_settings_clicked(self):
        logger.debug('Handle act_settings clicked')
        self.app.settings_window.show()
        self.app.settings_window.exec()

    def on_act_quit_clicked(self):
        logger.debug('Handle act_quit clicked')
        self.app.quit()

    def showEvent(self, event):
        self.act_enable.setChecked(self.app.clipboard_monitor.enable)
        self.set_trans_handler(self.settings.trans_mode, self.settings.trans_api)

    @Slot(bool)
    def set_enable_handler(self, enable):
        logger.debug(f'Handle set_enable, enable={enable}')
        self.act_enable.setChecked(enable)

    @Slot(str, str)
    def set_trans_handler(self, trans_mode, trans_api):
        logger.debug(f'Handle set_trans, trans_mode={trans_mode}, trans_api={trans_api}')
        self.set_trans_mode_handler(trans_mode)
        self.set_trans_api_handler(trans_api)

    @Slot(str)
    def set_trans_mode_handler(self, trans_mode):
        logger.debug(f'Handle set_trans_mode, trans_mode={trans_mode}')
        # self.settings.trans_mode = trans_mode
        trans_mode_query = {
            'auto': self.act_auto,
            'zh_en': self.act_zh_en,
            'zh_jp': self.act_zh_jp
        }

        try:
            trans_mode_menu = trans_mode_query[trans_mode]
            trans_mode_menu.setChecked(True)
        except KeyError:
            logger.exception(f'Invalid trans mode, trans_mode={trans_mode}')

    @Slot(str)
    def set_trans_api_handler(self, trans_api):
        logger.debug(f'Handle set_trans_api, trans_api={trans_api}')
        # self.settings.trans_api = trans_api
        trans_api_query = {
            'Baidu': self.act_baidu,
            'Youdao': self.act_youdao
        }

        try:
            trans_api_menu = trans_api_query[trans_api]
            trans_api_menu.setChecked(True)
        except KeyError:
            logger.exception(f'Invalid trans API, trans_api={trans_api}')

    def trans_settings(self, *, trans_mode=None, trans_api=None):
        if trans_mode is not None:
            self.settings.trans_mode = trans_mode
        if trans_api is not None:
            self.settings.trans_api = trans_api

        self.set_trans_handler(self.settings.trans_mode, self.settings.trans_api)
        self.set_trans.emit(self.settings.trans_mode, self.settings.trans_api)
